﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection ;
using System.Drawing;
using System.ComponentModel;
using System.Xml.Serialization;
using NursingFormSystem;

namespace NursingFormSystem
{
    /// <summary>
    /// Y坐标轴信息
    /// </summary>
    [Serializable]
    [System.Runtime.InteropServices.ComVisible(false)]
    public class YAxisInfo
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public YAxisInfo()
        {
        }

        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <param name="_title"></param>
        /// <param name="_unit"></param>
        /// <param name="_valueMember"></param>
        /// <param name="_maxValue"></param>
        /// <param name="_minValue"></param>
        /// <param name="_symbolStyle"></param>
        /// <param name="_symbolColor"></param>
        public YAxisInfo(
            string _title,//名称
            string _unit,//单位
            ValueMemberType _valueMember,
            float _maxValue, 
            float _minValue,
            PointSymbolStyle _symbolStyle ,
            Color _symbolColor,
            int _Types,
            int _Show
            )
        {
            this.Title = _title;
            this.Unit = _unit;
            this.ValueMember = _valueMember;
            this.MaxValue = _maxValue;
            this.MinValue = _minValue;
            this.SymbolStyle = _symbolStyle;
            this.SymbolColor = _symbolColor;
            this.Types=_Types;
            this.Show=_Show;
        }

        private string _Title = null;
        /// <summary>
        /// 标题
        /// </summary>
        [DefaultValue( null )]
        public string Title
        {
            get { return _Title; }
            set { _Title = value; }
        }

        private string _Unit = null;
        /// <summary>
        /// 单位
        /// </summary>
        [DefaultValue(null)]
        public string Unit
        {
            get { return _Unit; }
            set { _Unit = value; }
        }

        private ValueMemberType _ValueMember = ValueMemberType.Body_Temperature ;
        /// <summary>
        /// 数据成员
        /// </summary>
        [DefaultValue( ValueMemberType.Body_Temperature )]
        public ValueMemberType ValueMember
        {
            get { return _ValueMember; }
            set { _ValueMember = value; }
        }

        private float _MaxValue = 0f;
        /// <summary>
        /// 最大值
        /// </summary>
        [DefaultValue( 0f )]
        public float MaxValue
        {
            get { return _MaxValue; }
            set { _MaxValue = value; }
        }

        private float _MinValue = 0;
        /// <summary>
        /// 最小值
        /// </summary>
        [DefaultValue( 0f)]
        public float MinValue
        {
            get { return _MinValue; }
            set { _MinValue = value; }
        }

        private PointSymbolStyle _SymbolStyle = PointSymbolStyle.SolidCicle;
        /// <summary>
        /// 数据点符号类型
        /// </summary>
        [DefaultValue( PointSymbolStyle.SolidCicle )]
        public PointSymbolStyle SymbolStyle
        {
            get { return _SymbolStyle; }
            set { _SymbolStyle = value; }
        }

        private Color _SymbolColor = Color.Red;
        /// <summary>
        /// 数据点符号颜色
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public Color SymbolColor
        {
            get { return _SymbolColor; }
            set { _SymbolColor = value; }
        }
        private int _Types = 0;
        /// <summary>
        /// 类别
        /// </summary>
        [DefaultValue( 0 )]
        public int Types
        {
            get { return _Types; }
            set { _Types = value; }
        }
         private int _Show = 0;
        /// <summary>
        /// 是否在体温单显示
        /// </summary>
        [DefaultValue( 0 )]
        public int Show
        {
            get { return _Show; }
            set { _Show = value; }
        }
        /// <summary>
        /// 文本形式的颜色值
        /// </summary>
        [Browsable( false )]
        [XmlElement]
        [DefaultValue("Red")]
        public string SymbolColorValue
        {
            get
            {
                return ColorTranslator.ToHtml(this.SymbolColor);
            }
            set
            {
                this.SymbolColor = ColorTranslator.FromHtml(value);
            }
        }


        //private float _SymbolSize = 20;
        ///// <summary>
        ///// 数据点符号大小
        ///// </summary>
        //[DefaultValue( 20 )]
        //public float SymbolSize
        //{
        //    get { return _SymbolSize; }
        //    set { _SymbolSize = value; }
        //}

        /// <summary>
        /// Y坐标轴区域左边界位置
        /// </summary>
        [NonSerialized]
        internal float ViewLeft = 0f;
        /// <summary>
        /// Y坐标轴文本在视图中占据的宽度
        /// </summary>
        [NonSerialized]
        internal float ViewWidth = 0f;

        public float GetPosition(float maxPos, float minPos , float value )
        {
            if( float.IsNaN( value ))
            {
                return float.NaN ;
            }
            if (maxPos > minPos)
            {
                float rate = (maxPos - minPos) / (_MaxValue - _MinValue);
                return minPos + (value - _MinValue) * rate;
            }
            else
            {
                return maxPos;
            }
        }

        /// <summary>
        /// 上一次数据点
        /// </summary>
        [NonSerialized]
        internal PointF LastPoint = new PointF(float.NaN, float.NaN);

        /// <summary>
        /// 上一次数据的图形的半径----用于直线描绘
        /// </summary>
        [NonSerialized]
        internal float LastSymbolSize = 0.0f;
        
        internal float GetValue(Measure instance,float TempValue,float TopTemp)
        {
            if (instance == null)
            {
                return float.NaN;
            }
            switch (this.ValueMember)
            {
                case ValueMemberType.Body_Temperature  :
                    return string.IsNullOrEmpty(instance.Body_Temperature) ? 
                        float.NaN : Convert.ToSingle(instance.Body_Temperature.Replace("E", "").Replace("e", "").Replace("Y", "").Replace("y", "").Replace("K", "").Replace("k", "").Replace("G", "").Replace("g", ""));//过滤耳温、腋温、口温、肛温、起搏器特殊字符
                case ValueMemberType.Breath :
                    return string.IsNullOrEmpty(instance.Breath) ?
                        float.NaN : Convert.ToSingle(instance.Breath.Replace("R", "").Replace("r", ""));
                case ValueMemberType.FT :
                    return string.IsNullOrEmpty(instance.FT) ? 
                        float.NaN :Convert.ToSingle(instance.FT.Replace("E", "").Replace("e", "").Replace("Y", "").Replace("y", "").Replace("K", "").Replace("k", "").Replace("G", "").Replace("g", ""));
                case ValueMemberType.Pluse :
                    return string.IsNullOrEmpty(instance.Pluse)?
                        float.NaN : Convert.ToSingle(instance.Pluse.Replace("H", "").Replace("h", ""));
                case ValueMemberType.HeartRate:
                    return string.IsNullOrEmpty(instance.HeartRate)?
                        float.NaN : Convert.ToSingle(instance.HeartRate);
                case ValueMemberType.PainScore:
                    return string.IsNullOrEmpty(instance.PainScore)?
                        float.NaN : Convert.ToSingle(instance.PainScore);
                case ValueMemberType.RT:
                    return string.IsNullOrEmpty(instance.RT)?
                        float.NaN : Convert.ToSingle(instance.RT);
                case ValueMemberType.DropedBeat:
                    return string.IsNullOrEmpty(instance.DropedBeat)?
                        float.NaN : Convert.ToSingle(instance.DropedBeat);
                case ValueMemberType.Respirator:
                    return string.IsNullOrEmpty(instance.Respirator)?
                        float.NaN : Convert.ToSingle(instance.Respirator);
            }
            return float.NaN;
        }

        internal string GetTemperatureValue(Measure instance)
        {
            if (instance == null)
            {
                return string.Empty;
            }
            switch (this.ValueMember)
            {
                case ValueMemberType.Body_Temperature:
                    if (instance.Body_Temperature.Contains("E") || instance.Body_Temperature.Contains("e"))//耳温
                    {
                        return "耳温";
                    }
                    else if (instance.Body_Temperature.Contains("Y") || instance.Body_Temperature.Contains("y"))//腋温
                    {
                        return "腋温";
                    }
                    else if (instance.Body_Temperature.Contains("K") || instance.Body_Temperature.Contains("k"))//口温
                    {
                        return "口温";
                    }
                    else if (instance.Body_Temperature.Contains("G") || instance.Body_Temperature.Contains("g"))//肛温
                    {
                        return "肛温";
                    }
                    else//默认腋温
                    {
                        return "腋温";
                    }
                case ValueMemberType.Breath:
                    if (instance.Breath.Contains("R") || instance.Breath.Contains("r"))//耳温
                    {
                        return "呼吸机";
                    }else
                        return string.Empty;
                case ValueMemberType.FT:
                    return string.Empty;
                case ValueMemberType.Pluse:
                    if (instance.Pluse.Contains("H") || instance.Pluse.Contains("h"))//起搏器
                    {
                        return "起搏器";
                    }
                    return string.Empty;
                case ValueMemberType.PainScore:
                    return string.Empty;
                case ValueMemberType.HeartRate:
                    return string.Empty;
            }
            return string.Empty;
        }

    }

    /// <summary>
    /// Y坐标轴信息列表
    /// </summary>
    [Serializable]
    [System.Runtime.InteropServices.ComVisible(false)]
    public class YAxisInfoList : List<YAxisInfo>
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public YAxisInfoList()
        {
        }

        /// <summary>
        /// 获得指定数据类型的坐标轴信息
        /// </summary>
        /// <param name="type">指定的数据类型</param>
        /// <returns>获得的坐标轴信息</returns>
        public YAxisInfo GetItem(ValueMemberType type)
        {
            foreach (YAxisInfo info in this)
            {
                if (info.ValueMember == type)
                {
                    return info;
                }
            }
            return null;
        }

        ///// <summary>
        ///// 为取值范围而修正对象数值
        ///// </summary>
        ///// <param name="m">数据</param>
        ///// <returns>操作是否修改了对象数据</returns>
        //public bool FixRange(Measure m)
        //{
        //    bool result = false;
        //    foreach (YAxisInfo info in this)
        //    {
        //        float v = info.GetValue( m,39f );
        //        bool changed = false ;
        //        if( float.IsNaN( v ) == false )
        //        {
        //            if( float.IsNaN( info.MaxValue ) == false 
        //                && v >info.MaxValue )
        //            {
        //                v = info.MaxValue ;
        //                changed = true ;
        //            }
        //            else if( float.IsNaN( info.MinValue ) == false 
        //                && v < info.MinValue )
        //            {
        //                v = info.MinValue ;
        //                changed = true ;
        //            }
        //            if( changed )
        //            {
        //                switch( info.ValueMember )
        //                {
        //                    case  ValueMemberType.Body_Temperature :
        //                        result = true ;
        //                        m.Body_Temperature = v.ToString() ;
        //                        break;
        //                    case  ValueMemberType.Breath :
        //                        result = true ;
        //                        m.Breath = v.ToString();
        //                        break;
        //                    case ValueMemberType.FT :
        //                        result = true ;
        //                        m.FT = v.ToString();
        //                        break;
        //                    case ValueMemberType.Pluse :
        //                        result = true ;
        //                        m.Pluse = v.ToString();
        //                        break;
        //                    case ValueMemberType.PainScore:
        //                        result = true;
        //                        m.PainScore = v.ToString();
        //                        break;
        //                    case ValueMemberType.HeartRate:
        //                        result = true;
        //                        m.HeartRate = v.ToString();
        //                        break;
        //                }
        //            }
        //        }//if
        //    }//foreach
        //    return result;
        //}

    }

    /// <summary>
    /// 点符号样式
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(false)]
    public enum PointSymbolStyle
    {
        /// <summary>
        /// 实心圆
        /// </summary>
        SolidCicle ,
        /// <summary>
        /// 空心圆
        /// </summary>
        HollowCicle,
        /// <summary>
        /// 交叉线
        /// </summary>
        Cross,
        /// <summary>
        /// 交叉圆
        /// </summary>
        CrossCicle,
        /// <summary>
        /// 起搏器
        /// </summary>
        PluseShow,
        /// <summary>
        /// 呼吸机
        /// </summary>
        BreathShow
    }

    [System.Runtime.InteropServices.ComVisible(false)]
    public enum ValueMemberType
    {
        /// <summary>
        /// 体温
        /// </summary>
        Body_Temperature,
        /// <summary>
        /// 物理降温
        /// </summary>
        FT,
        /// <summary>
        /// 脉搏
        /// </summary>
        Pluse,
        /// <summary>
        /// 心率
        /// </summary>
        HeartRate,
        /// <summary>
        /// 呼吸
        /// </summary>
        Breath,
        /// <summary>
        /// 疼痛评分
        /// </summary>
        PainScore,
        /// <summary>
        /// 复试体温
        /// </summary>
        RT,
        /// <summary>
        /// 脉搏短绌
        /// </summary>
        DropedBeat,
        /// <summary>
        /// 呼吸机
        /// </summary>
        Respirator,
        ///// <summary>
        ///// 起搏心率
        ///// </summary>
        //PacedRate
    }
    
}
